package com.bignerdranch.android.ecar.Adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.bignerdranch.android.ecar.R;
import com.bignerdranch.android.ecar.Utils.MyImageCache;
import com.bignerdranch.android.ecar.Utils.RequestUtil;

import org.json.JSONArray;
import org.json.JSONException;

import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2016/7/28.
 */
public class NewsAdapter extends BaseAdapter {
    private Context mContext;
    private LayoutInflater mInflater;
    private JSONArray mData;

    public NewsAdapter(Context context, JSONArray data) {
        mContext = context;
        mData = data;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
//        Log.e("", "==" + mData.length());
        return mData.length();
    }

    @Override
    public Object getItem(int position) {
        try {
            return mData.get(position);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
//        Log.e("", "getView===" + position);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.news_item, null);
        }
        ViewHold hold = new ViewHold();
        if (convertView.getTag() == null) {
            hold.title = (TextView) convertView.findViewById(R.id.news_title);
            hold.time = (TextView) convertView.findViewById(R.id.time);
            hold.visitor = (TextView) convertView.findViewById(R.id.visitor);
            hold.news_img = (NetworkImageView) convertView.findViewById(R.id.news_img);

            convertView.setTag(hold);
        } else {
            hold = (ViewHold) convertView.getTag();
        }

        try {
            SimpleDateFormat sf = new SimpleDateFormat("HH 小时前");
            Date date = new Date();
            long time_now = date.getTime();
            String time_edit = mData.getJSONObject(position).getString("edit_time");
            long time_old = Integer.parseInt(time_edit);
            long time = time_now - time_old;




            hold.title.setText(mData.getJSONObject(position).getString("title"));
            hold.time.setText( sf.format(time));


            hold.visitor.setText(mData.getJSONObject(position).getString("reply_count"));

            ImageLoader loader = new ImageLoader(RequestUtil.getRequestQueue(mContext), new MyImageCache());
            hold.news_img.setDefaultImageResId(R.drawable.sample);
            hold.news_img.setErrorImageResId(R.drawable.sample);
            hold.news_img.setImageUrl(mData.getJSONObject(position).getString("top_image"), loader);
//            Log.e("====time=", time / 1000 + "s");
//            Log.e("==title===", mData.getJSONObject(position).getString("title"));
//            Log.e("==reply_count===", mData.getJSONObject(position).getString("reply_count"));
        } catch (JSONException e) {
            e.printStackTrace();
//            Log.e("", "" + e.toString());
        }


        return convertView;
    }

    private static class ViewHold {
        private NetworkImageView news_img;
        private TextView title;
        private TextView time;
        private TextView visitor;

    }
}
